package stu.khy.ssm.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import stu.khy.ssm.entity.DevUser;
import stu.khy.ssm.service.DevUserService;
import tech.wzd.tools.Constants;

@Controller
//为了和其他的controller有一个更好的划分
@RequestMapping(value = "/dev")
public class DevUserController {
	private Logger logger = Logger.getLogger(DevUserController.class);
	@Resource
	/* @Autowired */
	private DevUserService devUserService;

	public void setDevUserService(DevUserService devUserService) {
		this.devUserService = devUserService;
	}
   //第一张页面
	@RequestMapping(value = "/firstpage.html")
	public String login() {
		logger.debug("LoginController welcome ApplictionSystem");
		return "firstpage";
	}
	@RequestMapping(value = "/devlogin.html")
	public String devlogin() {
		logger.debug("登录入口");
		return "devlogin";
	}
	// 登录 开发者平台入口
	@RequestMapping(value = "/dologin.html")
	public String dologin(@RequestParam String devCode,@RequestParam String devPassword,HttpServletRequest request,HttpSession session) {
		logger.debug("dologin");
		DevUser user = null;
		try {
			// 调用service方法进行用户匹配
			user = devUserService.login(devCode,devPassword);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		if (user != null) {
			// 放入session
			// Constants.DEV_USER_SESSION
			session.setAttribute(Constants.DEV_USER_SESSION,user);
			// 进行页面跳转(main.jsp)
			return "redirect:/dev/flatform/main";
		} else {
			request.setAttribute("error", "用户名或密码不正确");
			return "devlogin";
		}

	}

	// 到主页面
	@RequestMapping(value = "/flatform/main")
	public String main(HttpSession session) {
		if (session.getAttribute("user")== null) {
			return "redirect:/dev/login";
		}
		return "developer/main";
	}

	// 注销
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		// 清楚session
		session.removeAttribute("user");
		return "devlogin";
	}
	
}
